const { ccclass, property } = cc._decorator;
import DetailControl from './DetailControl';
import UpgradeControl from './UpgradeControl';
import GradeControl from './GradeControl';
import ScrollSelect from './ScrollSelect';

class Grade {
    exp: number = 1000;
    money: number = 100;
    ftg: number = 30;
    ftgMax: number = 100;
}
class Detail {
    Id: string = "";
    Name: string = "";
    Prefab: string = "";
    Upexp: number = 0;//升级需要的Exp
    Grade: number = 1;//等级
    Life: number = 150;//生命值
    Damage: number = 15;//攻击力
    ArmorC: number = 10;//护甲值
    ArmorA: number = 10;//魔抗值
    LifeRe: number = 0.8;//生命回复
    RunSpeed: number = 0;
    JumpSpeed: number = 0;
}

@ccclass
export default class DetailsManager extends cc.Component {
    @property(cc.ParticleSystem)
    loveAnim: cc.ParticleSystem = null;
    @property(cc.Node)
    NameNode: cc.Node = null;
    @property(cc.Node)
    upgradeNode: cc.Node = null;
    @property(cc.Node)
    detailsNode: cc.Node = null;
    @property(cc.Node)
    grade: cc.Node = null;
    @property(cc.Node)
    heroNode: cc.Node = null;
    _heroIcons: cc.SpriteAtlas = null;

    detailControl: DetailControl = null;
    _heroDetails: any = null;
    _gameHelper: gameHelper = null;
    _heroId: string = "";
    _Grade: GradeControl = null;
    onLoad() {
        this._gameHelper = gameHelper.getInstance();
        this.upgradeNode.active = false;
        this.detailControl = this.detailsNode.getComponent(DetailControl);
        this.loveAnim.stopSystem();
        this.initRecoverFtg(0.5);
        this.schedule(this.recoverFtg, 120);
    }
    start() {
        this._Grade = this.grade.getComponent(GradeControl);
        this._Grade.init(this.getGrade());
        this.node.on('Upgrade', function (event: cc.Event.EventCustom) {
            let date = event.getUserData();
            this.onUpgrade(date);
            event.stopPropagation();
        }.bind(this));
    }
    setHeroScrol() {
        let atlas: cc.SpriteFrame[] = [];
        let ids: string[] = [];
        let heroDetails = this._heroDetails.Heros;
        let heros = gameHelper.getInstance().myHeroDetails;
        let self = this;
        if (heros?.length > 0) {
            heros.forEach(element => {
                for (let i = 0; i < heroDetails.length; i++) {
                    if (heroDetails[i].Id == element.Id) {
                        atlas.push(self._heroIcons.getSpriteFrame(heroDetails[i].Details.Sptite));
                        ids.push(element.Id);
                        break;
                    }
                }
            });
        }
        else {
            let element = heroDetails[0];
            let newHero = this.newDetail(element.Id);
            this.setDetail(newHero);
            atlas.push(this._heroIcons.getSpriteFrame(element.Details.Sptite));
            ids.push(element.Id);
        }
        let ss = this.heroNode.getComponent(ScrollSelect);
        ss.atlas = atlas;
        ss.ids = ids;
    }
    addHeroScrol(id: string) {
        let newHero = this.newDetail(id);
        this.setDetail(newHero);
        this.setHeroScrol();
        let ss = this.heroNode.getComponent(ScrollSelect);
        ss.reLoadChildren();
    }
    //更新显示信息
    loaddetail(event) {
        let deta = this.getDetail(event.id);
        if (deta) {
            this._gameHelper.heroDetail = deta;//this.getheroDetail(deta.id).details;
            this._gameHelper.heroId = deta.Id;
            this.setName(deta.Name);
            this.setdetail(deta);
            this._heroId = deta.Id;
            this.setupgrade();
        }
    }
    setHeroParam(detail: any) {
        let heroM = HeroManager.getInstance();
        heroM.runSpeed = detail.RunSpeed;
        heroM.jumpSpeed = detail.JumpSpeed;
        heroM.lifeMax = heroM.life = detail.Life;
        heroM.lifeRegain = detail.LifeRe;
        heroM.CommonImmunity = detail.ArmorA;
        heroM.AttributeImmunity = detail.ArmorC;
        heroM.damageAttack = detail.Damage;
    }
    //设置姓名显示
    setName(name: string) {
        this.NameNode.getComponent(cc.Label).string = name;
    }
    //设置属性信息显示
    setdetail(detail: any) {
        this.setHeroParam(detail);
        this.detailControl.setvalue(detail);
    }
    //设置升级按钮
    setupgrade(id?: string, exp?: number) {
        if (!id)
            id = this._heroId;
        if (!exp || exp == 0)
            exp = this.getExp(id);
        if (exp <= this.getGrade().exp) {
            this.upgradeNode.active = true;
            this.upgradeNode.getComponent(UpgradeControl).SetExp(id, exp);
        }
        else {
            this.upgradeNode.active = false;
        }
    }
    //触发升级
    onUpgrade(id: string) {
        let exp = this.getExp(id);
        let newexp = this.disburseExp(exp);
        if (newexp < 0)
            return;
        this.grade.getComponent(GradeControl).showExp(newexp);
        let detail = this.onUpgradeDetail(id);
        if (detail) {
            this.setdetail(detail);
            this.setupgrade(id, this.getExp(id));
        }
        this.loveAnim.resetSystem();
        this.scheduleOnce(() => { this.loveAnim.stopSystem() }, 0.5);
    }
    //重命名
    reName(name: string, id?: string) {
        if (!id)
            id = this._heroId;
        let myDetails: Array<Detail> = this._gameHelper.myHeroDetails;

        for (let i = 0; i < myDetails.length; i++) {
            if (id == myDetails[i].Id) {
                myDetails[i].Name = name;
            }
        }
        this._gameHelper.myHeroDetails = myDetails;
    }
    //获取英雄需要的升级Exp
    getExp(id: string): number {
        return this.getDetail(id).Upexp;
    }
    //升级
    onUpgradeDetail(id: string): any {
        let detail = this.getDetail(id);
        let element = this.getheroDetail(id);
        detail.Grade++;
        detail.Life += element.Upgrade.Life;
        let _damage = detail.Damage + element.Upgrade.Damage;
        detail.Damage = parseFloat(_damage.toFixed(1));
        let _armorC = detail.ArmorC + element.Upgrade.ArmorC;
        detail.ArmorC = parseFloat(_armorC.toFixed(1));
        let _armorA = detail.ArmorA + element.Upgrade.ArmorA;
        detail.ArmorA = parseFloat(_armorA.toFixed(1));
        let lifere = detail.LifeRe + element.Upgrade.LifeRe;
        detail.LifeRe = parseFloat(lifere.toFixed(1));
        detail.Upexp += element.Upgrade.Exp;
        this.setDetail(detail);
        return detail;
    }
    getheroDetail(id: string): any {
        var date = null;
        this._heroDetails.Heros.some(function (element) {
            if (element.Id == id) {
                date = element;
                return true;
            }
        }.bind(this));
        return date;
    }
    //增加Exp
    addExp(exp: number) {
        let grade = this.getGrade();
        grade.exp += exp;
        this._gameHelper.gradeParam = grade;
        this._Grade.showExp(grade.exp);
        return grade.exp;
    }
    //消耗Exp
    disburseExp(exp: number): number {
        let grade = this.getGrade();
        if (grade.exp < exp)
            return -1;
        grade.exp -= exp;
        this._gameHelper.gradeParam = grade;
        this._Grade.showExp(grade.exp);
        return grade.exp;
    }
    //消耗Ftp
    disburseFtg(ftg: number): number {
        let grade = this.getGrade();
        if (grade.ftg < ftg)
            return -1;
        grade.ftg -= ftg;
        this._gameHelper.gradeParam = grade;
        this._Grade.showFtg(grade.ftg);
        return grade.ftg;
    }
    //增加Ftg
    addFtg(ftg: number) {
        let grade = this.getGrade();
        grade.ftg += ftg;
        this._gameHelper.gradeParam = grade;
        this._Grade.showFtg(grade.ftg);
        return grade.ftg;
    }
    //恢复体力值
    recoverFtg() {
        let grade = this.getGrade();
        if (grade.ftg < grade.ftgMax) {
            grade.ftg += 1;
            this._gameHelper.gradeParam = grade;
            this._Grade.showFtg(grade.ftg);
            gameHelper.getInstance().quitTime = new Date;
        }
    }
    //挂机恢复体力
    initRecoverFtg(s: number) {
        let p: Grade = gameHelper.getInstance().gradeParam;
        if (p.ftg > p.ftgMax)
            return;
        let timed: Date = new Date(gameHelper.getInstance().quitTime);
        if (timed) {

            let now: Date = new Date();
            gameHelper.getInstance().quitTime = now;
            if (now.getFullYear() == timed.getFullYear()) {
                if (now.getMonth() == timed.getMonth()) {
                    if (now.getDate() == timed.getDate()) {
                        let timeH = now.getHours() - timed.getHours();
                        let timeM = now.getMinutes() - timed.getMinutes();
                        let time = timeH * 60 + timeM;
                        if (time <= 1)
                            return;
                        p.ftg += Math.floor(time * s);
                        if (p.ftg > 100)
                            p.ftg = 100;
                        gameHelper.getInstance().gradeParam = p;
                        return;
                    }
                }
            }
            p.ftg = p.ftgMax;
            gameHelper.getInstance().gradeParam = p;
        }
        else {
            p.ftg = p.ftgMax;
            gameHelper.getInstance().gradeParam = p;
        }
    }
    //消耗金币
    disManey(money: number): number {
        let grade = this.getGrade();
        if (grade.money < money)
            return -1;
        grade.money -= money;
        this._gameHelper.gradeParam = grade;
        this._Grade.showMoney(grade.money);
        return grade.money;
    }
    //获取经验信息
    getGrade(): Grade {
        return this._gameHelper.gradeParam;
    }
    //获取英雄的等级和属性信息
    getDetail(id: string): Detail {
        let myDetails: Array<Detail> = this._gameHelper.myHeroDetails;
        if (!myDetails) {
            myDetails = [this.newDetail(id)];
            this._gameHelper.myHeroDetails = myDetails;
            return myDetails[0];
        }
        else {
            let date = null;
            if (myDetails) {
                myDetails.some((element) => {
                    if (id == element?.Id) {
                        date = element;
                        return true;
                    }
                });
            }
            if (!date) {
                date = this.newDetail(id)
            }
            return date;
        }
    }
    setDetail(detail: Detail) {
        if (!this._gameHelper)
            this._gameHelper = gameHelper.getInstance();
        let myDetails: Array<Detail> = this._gameHelper.myHeroDetails;
        if (myDetails.length > 0) {
            let details = myDetails.filter(item => item.Id != detail.Id);
            details.push(detail);
            this._gameHelper.myHeroDetails = details;
        }
        else {
            myDetails.push(detail);
        }
    }
    newDetail(id: string): any {
        for (let i = 0; i < this._heroDetails.Heros.length; i++) {
            let hero = this._heroDetails.Heros[i];
            if (hero.Id == id) {
                let date = new Detail();
                date.Id = id;
                date.Prefab = hero.Details.Prefab;
                date.ArmorA = hero.Details.ArmorA;
                date.ArmorC = hero.Details.ArmorC;
                date.Damage = hero.Details.Damage;
                date.Grade = hero.Details.Grade;
                date.Life = hero.Details.Life;
                date.LifeRe = hero.Details.LifeRe;
                date.Name = hero.Details.Name;
                date.Upexp = hero.Details.Upexp;
                date.RunSpeed = hero.Details.RunSpeed;
                date.JumpSpeed = hero.Details.JumpSpeed;
                return date;
            }
        }
        return null;
    }
}
